//
//  XZNewsVC.m
//  Teecloud
//
//  Created by PC on 2018/10/16.
//  Copyright © 2018年 小志. All rights reserved.
//
#import <SafariServices/SafariServices.h>
#import "XZNewsVC.h"
#import "XZSearchVC.h"
#import "XZNewsCaseVC.h"
#import "XZGroupIntroduceVC.h"
#import "XZMyGroupDetailVC.h"
#import "GMBuyVideoVC.h"
#import "GMBuyFile.h"
#import "GMReadPDFVC.h"
#import "GMExercisesVC.h"
#import "GMExaminationVC.h"
#import "GMCoursePackageVC.h"
#import "GMPlayerViewController.h"

#import "XZNewsHeadView.h"
#import "GMHomeImgCell.h"
#import "GMNewsViewCell.h"

#import "XZNewsList.h"
#import "XZNewsBanner.h"
#import "XZBannerTops.h"

@interface XZNewsVC ()<UITableViewDataSource,UITableViewDelegate,SFSafariViewControllerDelegate,NewsBannerDelegate>
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) XZNewsHeadView *headView;
@end

#define NEWSLISTCELL @"XZNewsHeadView"
#define NEWSBANNERCELL @"GMHomeImgCell"

@implementation XZNewsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"新闻资讯";
    self.view.backgroundColor = kWhiteColor;
    [self addRightBarButtonWithFirstImage:[UIImage imageNamed:@"search-white-64"] action:@selector(searchAction)];
    //导航栏logo
    UIImageView *img = [[UIImageView alloc]init];
    img.image = [[UIImage imageNamed:@"navIcon"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithCustomView:img];
    self.navigationItem.leftBarButtonItem = leftBar;
    
    [self pullNewsData];
}
#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    self.tableView.mj_footer.hidden = self.dataSource.count > 0? NO:YES;
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XZNewsList *model = self.dataSource[indexPath.row];
    UITableViewCell *cell = nil;
    if (!model.IsAd) {
        GMNewsViewCell *newsViewCell = [tableView dequeueReusableCellWithIdentifier:NEWSLISTCELL forIndexPath:indexPath];
        newsViewCell.model = model;
        cell = newsViewCell;
    }else{
        GMHomeImgCell *homeImageCell = [tableView dequeueReusableCellWithIdentifier:NEWSBANNERCELL];
        homeImageCell.newsModel = model;
        cell = homeImageCell;
    }
    return cell;
}
#pragma mark - TableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    XZNewsList *model = self.dataSource[indexPath.row];
    if (model.IsAd) {//广告 就更新阅读计数
        [self updateReadCount:model.Id];
    }
    [self skipControllerWithNewsListModel:model];
}
/************************点击cell进行跳转**********************/
/**
 根据模型中RelType来进行跳转
 cell
 @param model XZBannerTops
 */
- (void)skipControllerWithNewsListModel:(XZNewsList *)model{
    if (1 == [model.RelType integerValue]) {//model.RelType=1
        [self relTypeIsEqualOneSkipControllerWithNewsListModel:model];
    }else if (2 == [model.RelType integerValue]){//model.RelType=2
        [self skipOrganizationOrGroupWithNewsListModel:model];
    }else if (3 == [model.RelType integerValue]){//新闻资讯案例
        XZNewsCaseVC *newsCaseVC = [[XZNewsCaseVC alloc]init];
        newsCaseVC.newsId = model.Id;
        [self.navigationController pushViewController:newsCaseVC animated:true];
    }else if (4 == [model.RelType integerValue]){//广告详情页面基地址
        NSString *adUrlString = [NSString stringWithFormat:@"%@/%@",URL_ADURL,model.Id];
        SFSafariViewController *sfVC = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:adUrlString]];
        sfVC.title = model.Name;
        sfVC.delegate = self;
        [self presentViewController:sfVC animated:YES completion:nil];
    }
}

/*
 **如model.RelType=1
 *进行相应的跳转
 */
- (void)relTypeIsEqualOneSkipControllerWithNewsListModel:(XZNewsList *)model{
    if (0 == [model.NormalCode integerValue]) {
        [self skipNormalCodeIsZeroControllerWithNewsListModel:model];
    }else if (1 == [model.NormalCode integerValue]){
        [MBProgressHUD showErrorOrSuc:@"关联资源已下架或不可用" toView:self.view];
    }else if (2 == [model.NormalCode integerValue]){
        [MBProgressHUD showErrorOrSuc:@"当前手机型号非购买资源所用手机型号，你可以到“我的 - 主页 - 个人资料(付费资源管理)”模块申请变更手机型号" toView:self.view];
    }
}
/*
 *当model.NormalCode=0时 根据ResType及IsAdded进行相应的跳转
 */
- (void)skipNormalCodeIsZeroControllerWithNewsListModel:(XZNewsList *)model{
    if (1 == [model.ResType integerValue]) {//视频
        if (!model.IsFree) {
            GMBuyVideoVC *buyVideoVC = [[GMBuyVideoVC alloc]init];
            buyVideoVC.ResType = model.ResType;
            buyVideoVC.resId = model.Id;
        
            [self.navigationController pushViewController:buyVideoVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 试看(视频)“页面" toView:self.view];
        }else{
            GMPlayerViewController *playerVC = [[GMPlayerViewController alloc]init];
            playerVC.resId = model.Id;
            [self.navigationController pushViewController:playerVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 播放 - 问题“页面" toView:self.view];
        }
    }else if (2 == [model.ResType integerValue]){//文档
        if (!model.IsFree) {
            GMBuyFile * buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
         
            buyFile.resId = model.Id;
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(文档)“页面" toView:self.view];
        }else{
            GMReadPDFVC *myFileVC = [[GMReadPDFVC alloc]init];
            myFileVC.resId = model.Id;
            [self.navigationController pushViewController:myFileVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 文档“页面" toView:self.view];
        }
    }else if (3 == [model.ResType integerValue]){//习题
        if (!model.IsFree) {
            GMBuyFile *buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
            buyFile.resId = model.Id;
        
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(习题、试卷)“页面" toView:self.view];
        }else{
#warning fromType不知道传什么
            GMExercisesVC *exercisesVC = [[GMExercisesVC alloc]init];
            exercisesVC.resId = model.Id;
//            fromType
            [self.navigationController pushViewController:exercisesVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 习题 - 答题“页面" toView:self.view];
        }
    }else if (4 == [model.ResType integerValue]){//试卷
        if (!model.IsFree) {
            GMBuyFile *buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
         
            buyFile.resId = model.Id;
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(习题、试卷)“页面" toView:self.view];
        }else{
            GMExaminationVC * examinationVC = [[GMExaminationVC alloc]init];
            examinationVC.resId = model.Id;
            [self.navigationController pushViewController:examinationVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 试卷考试 - 考试须知“页面" toView:self.view];
        }
    }else if (9 == [model.ResType integerValue]){//课程包
        if (!model.IsFree) {
            GMBuyFile *buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
          
            buyFile.resId = model.Id;
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(课程包)“页面" toView:self.view];
        }else{
            GMCoursePackageVC *coursePackageVC = [[GMCoursePackageVC alloc]init];
            coursePackageVC.resId = model.Id;
            coursePackageVC.titleStr = model.Name;
            [self.navigationController pushViewController:coursePackageVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 课程包“页面" toView:self.view];
        }
    }
}
/*
**如model.RelType=2
* true=IsAdded：跳转”主页 - 我的 - 我的社团/小组 - 详情“页面
* false=IsAdded：跳转”主页 - 资源 - 社团/小组 - 介绍“页面
 */
- (void)skipOrganizationOrGroupWithNewsListModel:(XZNewsList *)model{
    if (!model.IsAdded) {
        XZGroupIntroduceVC *groupIntroduceVC = [[XZGroupIntroduceVC alloc]init];
        groupIntroduceVC.titileStr = model.Name;
        groupIntroduceVC.groupId = model.Id;
        [self.navigationController pushViewController:groupIntroduceVC animated:true];
    }else{
        XZMyGroupDetailVC *myGroupDetailVC = [[XZMyGroupDetailVC alloc]init];
        myGroupDetailVC.titleStr = model.Name;
        myGroupDetailVC.groupId = model.Id;
        [self.navigationController pushViewController:myGroupDetailVC animated:true];
    }
    
}
#pragma mark - SFSafariViewControllerDelegate -
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller{
    XZLog(@"击done");
}
#pragma mark - NewsBannerDelegate
- (void)clickNewsBannerIndex:(XZBannerTops *)model{
    //更新广告阅读技术
    [self updateReadCount:model.AdId];
    [self bannerSkipControllerWithNewsBanner:model];
}
  /*************************************轮播跳转**************************************/
/**
 根据模型中RelType来进行跳转
 轮播
 @param model XZBannerTops
 */
- (void)bannerSkipControllerWithNewsBanner:(XZBannerTops *)model{
    if (1 == [model.RelType integerValue]) {//model.RelType=1
        [self bannerSkipRelTypeIsOne:model];
    }else if (2 == [model.RelType integerValue]){//model.RelType=2
        [self bannerSkipRelTypeIsTwo:model];
    }else if (3 == [model.RelType integerValue]){//新闻资讯案例
        XZNewsCaseVC *newsCaseVC = [[XZNewsCaseVC alloc]init];
        newsCaseVC.newsId = model.Id;
        [self.navigationController pushViewController:newsCaseVC animated:true];
    }else if (4 == [model.RelType integerValue]){//广告详情页面基地址
        NSString *adUrlString = [NSString stringWithFormat:@"%@/%@",URL_ADURL,model.Id];
        SFSafariViewController *sfVC = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:adUrlString]];
        sfVC.title = model.Title;
        sfVC.delegate = self;
        [self presentViewController:sfVC animated:YES completion:nil];
    }
}
/*
 *model.RelType=1 根据NormalCode  ResType IsAdded进行相应的跳转
 */
- (void)bannerSkipRelTypeIsOne:(XZBannerTops *)model{
    if (0 == [model.NormalCode integerValue]) {
        [self bannerSkipNormalCodeIsZeroControllerWithBannerTopsModel:model];
    }else if (1 == [model.NormalCode integerValue]){
        [MBProgressHUD showErrorOrSuc:@"关联资源已下架或不可用" toView:self.view];
    }else if (2 == [model.NormalCode integerValue]){
        [MBProgressHUD showErrorOrSuc:@"当前手机型号非购买资源所用手机型号，你可以到“我的 - 主页 - 个人资料(付费资源管理)”模块申请变更手机型号" toView:self.view];
    }
}
/*
 *当model.NormalCode=0时 根据ResType及IsAdded进行相应的跳转
 */
- (void)bannerSkipNormalCodeIsZeroControllerWithBannerTopsModel:(XZBannerTops *)model{
    if (1 == [model.ResType integerValue]) {//视频
        if (!model.IsFree) {
            GMBuyVideoVC *buyVideoVC = [[GMBuyVideoVC alloc]init];
            buyVideoVC.ResType = model.ResType;
            buyVideoVC.resId = model.Id;
         
            [self.navigationController pushViewController:buyVideoVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 试看(视频)“页面" toView:self.view];
        }else{
            GMPlayerViewController *playerVC = [[GMPlayerViewController alloc]init];
            playerVC.resId = model.Id;
   
            [self.navigationController pushViewController:playerVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 播放 - 问题“页面" toView:self.view];
        }
    }else if (2 == [model.ResType integerValue]){//文档
        if (!model.IsFree) {
            GMBuyFile * buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
          
            buyFile.resId = model.Id;
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(文档)“页面" toView:self.view];
        }else{
            GMReadPDFVC *myFileVC = [[GMReadPDFVC alloc]init];
            myFileVC.resId = model.Id;
            [self.navigationController pushViewController:myFileVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 文档“页面" toView:self.view];
        }
    }else if (3 == [model.ResType integerValue]){//习题
        if (!model.IsFree) {
            GMBuyFile *buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
            buyFile.resId = model.Id;
          
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(习题、试卷)“页面" toView:self.view];
        }else{
            GMExercisesVC *exercisesVC = [[GMExercisesVC alloc]init];
            exercisesVC.resId = model.Id;
            //            fromType
            [self.navigationController pushViewController:exercisesVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 习题 - 答题“页面" toView:self.view];
        }
    }else if (4 == [model.ResType integerValue]){//试卷
        if (!model.IsFree) {
            GMBuyFile *buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
          
            buyFile.resId = model.Id;
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(习题、试卷)“页面" toView:self.view];
        }else{
            GMExaminationVC * examinationVC = [[GMExaminationVC alloc]init];
            examinationVC.resId = model.Id;
            [self.navigationController pushViewController:examinationVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 试卷考试 - 考试须知“页面" toView:self.view];
        }
    }else if (9 == [model.ResType integerValue]){//课程包
        if (!model.IsFree) {
            GMBuyFile *buyFile = [[GMBuyFile alloc]init];
            buyFile.ResType = model.ResType;
         
            buyFile.resId = model.Id;
            [self.navigationController pushViewController:buyFile animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 资源 - 学习资源 - 详情(课程包)“页面" toView:self.view];
        }else{
            GMCoursePackageVC *coursePackageVC = [[GMCoursePackageVC alloc]init];
            coursePackageVC.resId = model.Id;
            coursePackageVC.titleStr = model.Title;
            [self.navigationController pushViewController:coursePackageVC animated:true];
//            [MBProgressHUD showErrorOrSuc:@"跳转”主页 - 我的 - 我的资源 - 课程包“页面" toView:self.view];
        }
    }
}
/*
 *model.RelType=2 根据 IsAdded进行相应的跳转
*/
- (void)bannerSkipRelTypeIsTwo:(XZBannerTops *)model{
    if (!model.IsAdded) {
        XZGroupIntroduceVC *groupIntroduceVC = [[XZGroupIntroduceVC alloc]init];
        groupIntroduceVC.titileStr = model.Title;
        groupIntroduceVC.groupId = model.Id;
        [self.navigationController pushViewController:groupIntroduceVC animated:true];
    }else{
        XZMyGroupDetailVC *myGroupDetailVC = [[XZMyGroupDetailVC alloc]init];
        myGroupDetailVC.titleStr = model.Title;
        myGroupDetailVC.groupId = model.Id;
        [self.navigationController pushViewController:myGroupDetailVC animated:true];
    }
}
#pragma mark - searchAction
- (void)searchAction{
    XZSearchVC *searchVC = [[XZSearchVC alloc]init];
    [self.navigationController pushViewController:searchVC animated:true];
}
#pragma mark - Setter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (XZNewsHeadView *)headView{
    if (!_headView) {
        _headView = [[XZNewsHeadView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 180)];
        _headView.delegate = self;
    }
    return _headView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT-kNavBarAndStatusBarHeight-kTabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;

        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GMNewsViewCell class]) bundle:nil] forCellReuseIdentifier:NEWSLISTCELL];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GMHomeImgCell class]) bundle:nil] forCellReuseIdentifier:NEWSBANNERCELL];
        //上下拉刷新
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullNewsData)];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addMoreNewsData)];
        _tableView.mj_footer.hidden =YES;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark - Networking
/*
 *更新广告阅读计数
 */
-(void)updateReadCount:(NSString *)addId{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    //广告Id
    [dict setValue:addId forKey:@"adId"];
    
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=UpdateAdCount&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
               XZLog(@"计数成功");
            }else{
                XZLog(@"计数失败");
            }
            
        }else{
            XZLog(@"%@",error);
        }
    }];
}
//轮播
- (void)getBannerData{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];

    __weak typeof(self)weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetNewsHomeInfo&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSDictionary *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                XZNewsBanner *model = [XZNewsBanner mj_objectWithKeyValues:dataInfo];
                weakSelf.headView.model = model;
            }else{
                
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
            
        }else{
            XZLog(@"%@",error);
        }
    }];
}
//下拉刷新
- (void)pullNewsData{
    self.pageIndex = 0;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:@(self.pageIndex) forKey:@"pageIndex"];
    
    __weak typeof(self)weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetNewsList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                //轮播数据
                [weakSelf getBannerData];
                weakSelf.tableView.tableHeaderView = self.headView;
                
                NSArray *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                NSArray *newsArray = [XZNewsList mj_objectArrayWithKeyValuesArray:dataInfo];

                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:newsArray];
                [weakSelf.tableView.mj_footer endRefreshing];
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView reloadData];
            }else{
                
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
            
        }else{
            XZLog(@"%@",error);
        }
    }];
}
//上拉加载更多
- (void)addMoreNewsData{
    self.pageIndex ++;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:@(self.pageIndex) forKey:@"pageIndex"];
    
    __weak typeof(self)weakSelf = self;
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetNewsList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                NSArray *newsArray = [XZNewsList mj_objectArrayWithKeyValuesArray:dataInfo];
                [weakSelf.dataSource addObjectsFromArray:newsArray];
                if (dataInfo.count>0) {
                    [weakSelf.tableView.mj_footer endRefreshing];
                    [weakSelf.tableView.mj_header endRefreshing];
                    [weakSelf.tableView reloadData];
                }else{//没有数据的时候就显示暂无数据
                    weakSelf.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                }
            }else{
                [weakSelf.tableView.mj_footer endRefreshing];
                [weakSelf.tableView.mj_header endRefreshing];
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakSelf.view];
            }
        }else{
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView.mj_header endRefreshing];
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakSelf.view];
        }
    }];
}
@end
